package com.atguigu.tingshu.user.pattern.impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.pattern.UserPaidStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 1001-专辑 1002-声音 1003-vip会员
 *
 * @author: atguigu
 * @create: 2024-03-09 11:34
 */
@Slf4j
@Component(SystemConstant.ORDER_ITEM_TYPE_ALBUM)  //默认beanId 类名-首字母小写 albumUserPaidStrategy 将beanID设置为跟业务提交购买类型一致
public class AlbumUserPaidStrategy implements UserPaidStrategy {

    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper;

    /**
     * 处理购买项目类型为：专辑
     *
     * @param userPaidRecordVo
     */
    @Override
    public void saveUserPaidRecord(UserPaidRecordVo userPaidRecordVo) {
        log.info("处理购买项目类型为：专辑,{}", userPaidRecordVo);
        //1.处理专辑购买记录
        //1.1 根据订单编号查询已购专辑记录 如果购买记录存在说明已经保存过则忽略
        LambdaQueryWrapper<UserPaidAlbum> userPaidAlbumLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userPaidAlbumLambdaQueryWrapper.eq(UserPaidAlbum::getOrderNo, userPaidRecordVo.getOrderNo());
        userPaidAlbumLambdaQueryWrapper.last("limit 1");
        Long count = userPaidAlbumMapper.selectCount(userPaidAlbumLambdaQueryWrapper);
        if (count > 0) {
            return;
        }
        //1.2 构建已购专辑记录对象，保存已购专辑
        UserPaidAlbum userPaidAlbum = new UserPaidAlbum();
        userPaidAlbum.setOrderNo(userPaidRecordVo.getOrderNo());
        userPaidAlbum.setUserId(userPaidRecordVo.getUserId());
        userPaidAlbum.setAlbumId(userPaidRecordVo.getItemIdList().get(0));
        userPaidAlbumMapper.insert(userPaidAlbum);
    }
}
